/**
 * Created by hhy on 2017/8/10.
 */
document.setTitle = function(t) {
    document.title = t;
    var i = document.createElement('iframe');
    i.src = '//m.baidu.com/favicon.ico';
    i.style.display = 'none';
    i.onload = function() {
        setTimeout(function(){
            i.remove();
        }, 9)
    }
    document.body.appendChild(i);
}
var vm = new Vue({
    el:'#ggapp',
    data:{
        hotList:[],
        article:{
            author:{"nickname":""}
        },
        comment:{
            "memberName":"游客"
        },
        commentList:[],
        tagList:[]
    },
    methods: {
        getHotList: function(){
            $.get(baseURL + "blog/article/hot", function(r){
                var al = r.data;
                for(var index in al){
                    var item = al[index];
                    item.createTime = moment(item.createTime).format('YYYY/MM/DD');
                    vm.hotList.push(item);
                }
            });
        },
        getArticle: function(id){
            $.get(baseURL + "blog/article/info/"+id, function(r){
                var article = r.data;
                article.createTime = moment(article.createTime).format('YYYY/MM/DD');
                article.tag = article.tag.split(",");
                vm.article = article;
            });
        },
        getTagList: function(){
            $.get(baseURL + "blog/tag/list", function(r){
                vm.tagList = r.data;
            });
        },
        getCommentList: function(articleId){
            $.get(baseURL + "comment/list?articleId="+articleId, function(r){
                vm.commentList = r.data;
            });
        },
        saveComment: function () {
            vm.comment.articleId = vm.article.id;
            $.ajax({
                type : "post",
                url : baseURL + "comment",
                contentType:"application/json",
                data : JSON.stringify(vm.comment),
                success : function(r){
                    if(r.httpCode == 200){
                        alert("评论成功！");
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
    },
    beforeCreate: function(){
        var articleId = getQueryString("id");
        if(articleId == null || articleId+"".trim().length == 0){
            location.href = "404.html";
        }
    },
    created: function(){
        var articleId = getQueryString("id");
        this.getArticle(articleId);
        this.getHotList();
        this.getTagList();
        this.getCommentList(articleId);
    },
    mounted:function(){

    }
});